**Load dataset
cd "Set directory"
use "learning.dta", clear

**Set globals
*Analyses sample
global sample if gap_t4!=. & posterior_beliefs_std!=. // Restrict sample to to all observations with non-missing outcomes
*Summary statistics
global summary level_t4 level_t0 posterior_beliefs_std prior_beliefs_std testresult_t4 gap_t4 preread
*Covariates and missing indicator
global covariates 	gender_nomiss  	age_nomiss 	non_western_nomiss 	basic_mom_nomiss 	upsec_mom_nomiss 	voc_mom_nomiss 	ac_mom_nomiss 	ba_mom_nomiss 	ma_mom_nomiss 	income_mom_nomiss 	unemployed_mom_nomiss 	employed_mom_nomiss outsidelm_mom_nomiss
global missing 		gender_miss		age_miss 	non_western_miss 		basic_mom_miss 		upsec_mom_miss 		voc_mom_miss 	ac_mom_miss 	ba_mom_miss 	ma_mom_miss 	income_mom_miss 	unemployed_mom_miss 	employed_mom_miss 	outsidelm_mom_miss 
*Premeasures
global premeasures prior_beliefs prior_uncertainty pre_readingspeed pre_explicit pre_implicit   pre_readcomposite
*Set cluster level_
global cluster school_id
*Set output
global output , // Displays all all output
*Set standard options for regression tabels
global options compress scalars("St Strata FE" "C Covariates" "r2_a Adjusted R^2") sfmt(2) cells(b(star fmt(a2)) se(fmt(a2) par) p(fmt(a2) par([ ]))) collabels(none)  label varwidth(45) nomtitle nogap nobase

**Table 1 Selection into Sample :
*Set missing values to missing on all covariates
foreach var in gender age non_western basic_mom upsec_mom voc_mom ac_mom ba_mom ma_mom income_mom unemployed_mom employed_mom outsidelm_mom {
replace `var'_nomiss = . if `var'_miss==1
}

*merge with population data
merge 1:1 student_id using "population_data.dta"
*Generate indicator for participation
gen participate=0
replace participate=1 if _merge==3

*Regress each covariate on participate indicator to obtain correct p-values (corrected for school cluster structure)
estimates clear 
foreach var of varlist  $covariates {
qui eststo: reg `var' participate, cluster(school_id)
}
capt mat drop _all
		qui esttab , se nostar label keep(participate)
		matrix C = r(coefs)
		eststo clear
		local models : coleq C
		local models : list uniq models
		local i 1
			local j 0 
			capt matrix drop b 
			capt matrix drop se
				foreach model of local models {
					local ++j
					matrix tmp = C[`i', 2*`j'-1]
					if tmp[1,1]<. {
						matrix colnames tmp = `model'
						matrix b = nullmat(b), tmp
						matrix tmp[1,1] = C[`i', 2*`j']
						matrix se = nullmat(se), tmp
					}
				}	
		ereturn post b 
		qui estadd matrix se 
		eststo treatment
		qui: esttab treatment, b(%9.2f) p nonum noobs label not mtitle("Difference")
		mat B = r(coefs)

*Obtain simple means and differences
	qui eststo D	: estpost ttest $covariates, by(participate)
	qui esttab D , cell(" mu_1(fmt(2) label(Not participating)) mu_2(fmt(2) label(Participating))") compres nogap varwidth(30) label nonum nomtitle //
	mat A = r(coefs) 

*Combine p-values with simple means and differences
	mat D = (A,B)
esttab mat(D, fmt(%9.2f)) ,  collabels("Not participating" "Participating" "Difference" "P-value") compres nogap varwidth(45) label  nonum nomtitle

*drop non-participants again:
drop if participate==0	

**Table 2: Balance at Baseline
*Regress each covariate on treatment indicator to obtain correct p-values (corrected for school cluster structure)
estimates clear 
foreach var of varlist  $covariates   $premeasures {
qui eststo: reg `var' treatment, cluster(school_id)
}
capt mat drop _all
		qui esttab , se nostar label keep(treatment)
		matrix C = r(coefs)
		eststo clear
		local models : coleq C
		local models : list uniq models
		local i 1
			local j 0 
			capt matrix drop b 
			capt matrix drop se
				foreach model of local models {
					local ++j
					matrix tmp = C[`i', 2*`j'-1]
					if tmp[1,1]<. {
						matrix colnames tmp = `model'
						matrix b = nullmat(b), tmp
						matrix tmp[1,1] = C[`i', 2*`j']
						matrix se = nullmat(se), tmp
					}
				}	
		ereturn post b 
		qui estadd matrix se 
		eststo treatment
	qui: esttab treatment, b(%9.2f) p nonum noobs label not mtitle("Difference")
	mat B = r(coefs)

*Obtain simple means and differences
	qui eststo D	: estpost ttest $covariates   $premeasures, by(treatment)
	qui esttab D , cell(" mu_1(fmt(2) label(Control)) mu_2(fmt(2) label(Treatment))") compres nogap varwidth(30) label nonum nomtitle //
	mat A = r(coefs)
	
*Combine p-values with simple means and differences
	mat D = (A,B)
esttab mat(D, fmt(%9.2f)), collabels("Control" "Treatment" "Difference" "P-value") compres nogap varwidth(45) label  nonum nomtitle

**Table 3: Balance in Sample of Analysis
*Regress each covariate on treatment indicator to obtain correct p-values (corrected for school cluster structure)
estimates clear 
foreach var of varlist  $covariates   $premeasures {
qui eststo: reg `var' treatment $sample, cluster(school_id)
}
capt mat drop _all
		qui esttab , se nostar label keep(treatment)
		matrix C = r(coefs)
		eststo clear
		local models : coleq C
		local models : list uniq models
		local i 1
			local j 0 
			capt matrix drop b 
			capt matrix drop se
				foreach model of local models {
					local ++j
					matrix tmp = C[`i', 2*`j'-1]
					if tmp[1,1]<. {
						matrix colnames tmp = `model'
						matrix b = nullmat(b), tmp
						matrix tmp[1,1] = C[`i', 2*`j']
						matrix se = nullmat(se), tmp
					}
				}	
	ereturn post b 
	qui estadd matrix se 
	eststo treatment
	qui: esttab treatment, b(%9.2f) p nonum noobs label not mtitle("Difference")
	mat B = r(coefs)

*Obtain simple means and differences
	qui eststo D	: estpost ttest $covariates   $premeasures $sample, by(treatment)
	qui esttab D , cell(" mu_1(fmt(2) label(Control)) mu_2(fmt(2) label(Treatment))") compres nogap varwidth(30) label nonum nomtitle //
	mat A = r(coefs) 

*Combine p-values with simple means and differences
	mat D = (A,B)
esttab mat(D, fmt(%9.2f)), collabels("Control" "Treatment" "Difference" "P-value") compres nogap varwidth(45) label  nonum nomtitle

*Set missing values to 0 for all covariates again:
foreach var in gender age non_western basic_mom upsec_mom voc_mom ac_mom ba_mom ma_mom income_mom unemployed_mom employed_mom outsidelm_mom {
replace `var'_nomiss = 0 if `var'_miss==1
}

**Table 4: Descriptive Statistics for Main Variables
*Summarize measures
estpost sum $summary $sample
mat H=e(mean)',e(sd)'

*Estimate all pairwise correlation
pwcorr  $summary $sample
mat A=r(C)
mata
A=st_matrix("A")
B=sublowertriangle(A,0)
st_matrix("P",B)
end
matlist P

*Combine summary statistics and pairwise correlations
mat J=H,P
matlist J
mat colnames J=Mean SD 1 2 3 4 5 6 7 
esttab mat(J, fmt(2)) , nomtitle label varwidth(30)

**Figure 3A)
reg posterior_beliefs_std c.testresult_t4##treatment c.prior_beliefs_std##treatment i.strata $covariates $missing $sample, cluster($cluster)
margins, dydx(  prior_beliefs_std testresult_t4) over(treatment)
mat a=r(table)
mat treat=a[1,2],a[1,4]
mat se=a[2,2],a[2,4]
ereturn post treat, obs(2028) dof(75)
estadd mat se
eststo treat
mat cont=a[1,1],a[1,3]
mat se=a[2,1],a[2,3]
ereturn post cont, obs(2028) dof(75)
estadd mat se
eststo cont

#delimit ;
coefplot (treat, se(se)) (cont, se(se)), levels(95 90) rename(c1="Test results (std.)" c2="Prior beliefs (std.)") scheme(s1mono) grid(none) 
	
	legend(order(3 "Treatment" 6 "Control" )  rows(2) region(lpattern(blank)) position(1) ring(0))  
	xlabel(0(.2)1.1) 
	addplot(
	(scatteri .82 .35 .82 .37 1.2 .37 1.2 .35, recast(connected) lpattern(solid) msymbol(none))
	(scatteri 1.82 .86 1.82 .88 2.2 .88 2.2 .86, recast(connected) lpattern(solid) msymbol(none))
	(scatteri 1 .37 "{&Delta}=.10*", msymbol(none) mlabsize(10-pt))
	(scatteri 2 .88 "{&Delta}=.078*", msymbol(none) mlabsize(10-pt))
	)
	xtitle("Partial Correlation with Posterior Beliefs")
	name(main_results, replace)
;
#delimit cr

**Figure 3b)
estimates clear
eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std i.strata  $covariates $missing $sample, cluster($cluster)
margins, at( gap_t4 =(-2(0.1)2) treatment=(0 1))
marginsplot, recast(line) recastci(rarea) ciopts(fcolor(%20) lcolor(%20))  scheme(s1mono) ///
xtitle(Gap) ytitle(Level of Difficulty) title(" ", ring(0)) name(level, replace)  xline(0, lpattern(dot)) ///
legend(order(4 "Treatment" 3 "Control" )  rows(2) region(lpattern(blank)) position(1) ring(0)) ///
text(.45 1 "Prior beliefs below test score" .45 -1 "Prior beliefs above test score") //

**Table 5/A2: Effect of Intervention on Posterior Beliefs
eststo clear
qui: eststo: reg posterior_beliefs_std c.testresult_t4##treatment c.prior_beliefs_std##treatment, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "No"
qui: eststo: reg posterior_beliefs_std c.testresult_t4##treatment c.prior_beliefs_std##treatment  $covariates $missing, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "Yes"
qui: eststo: reg posterior_beliefs_std c.testresult_t4##treatment c.prior_beliefs_std##treatment i.strata, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "No"
qui: eststo: reg posterior_beliefs_std c.testresult_t4##treatment c.prior_beliefs_std##treatment i.strata $covariates $missing, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "Yes"
esttab $output drop(*strata $missing) order(testresult_t4 prior_beliefs_std) coeflabels(1.treatment#c.testresult_t4 "Test result (std.) X Treatment " 1.treatment#c.prior_beliefs_std  "Prior belief (std.) X Treatment") $options
esttab $output drop(*strata $covariates $missing) order(testresult_t4 prior_beliefs_std) coeflabels(1.treatment#c.testresult_t4 "Test result (std.) X Treatment " 1.treatment#c.prior_beliefs_std  "Prior belief (std.) X Treatment") $options

**Table 6/A3: Effect of Intervention on Posterior Level of Action
estimates clear
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std  $sample, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "No"
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std i.strata  $sample, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "No"
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std $covariates $missing  $sample, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "Yes"
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std i.strata $covariates $missing  $sample, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "Yes"
esttab $output drop (*strata $missing) order(gap_t4) coeflabels(1.treatment#c.gap_t4 "Gap (test result-prior beliefs) X Treatment" gap_t4 "Gap (test result-prior beliefs)")  $options
esttab $output drop (*strata $missing $covariates) order(gap_t4) coeflabels(1.treatment#c.gap_t4 "Gap (test result-prior beliefs) X Treatment" gap_t4 "Gap (test result-prior beliefs)")  $options

**Table 7: Effect of Intervention on Posterior Action. Moderated by Uncertainty
estimates clear
eststo: reg level_t4 treatment##c.prior_uncertainty_std c.level_t0 prior_beliefs_std i.strata  $sample, cluster($cluster)
qui	estadd local St "Yes"
esttab $output  drop(*strata) sfmt(2) cells(b(star fmt(a2)) se(fmt(a2) par) p(fmt(a2) par([ ]))) collabels(none) label varwidth(40) nomtitle nogap nobase scalars("St Strata FE" "r2_a Adjusted R^2")

**Table 8: Effect of Intervention on Posterior Action. Moderated by Information Signal Consistency
estimates clear
eststo: reg level_t4 treatment##c.consistency c.level_t0 prior_beliefs_std i.strata  $sample, cluster($cluster)
qui	estadd local St "Yes"
esttab $output drop(*strata) sfmt(2) cells(b(star fmt(a2)) se(fmt(a2) par) p(fmt(a2) par([ ]))) collabels(none)  label varwidth(40) nomtitle nogap nobase scalars("St Strata FE" "r2_a Adjusted R^2")

**Table A1: Frequency table of teacher uncertainty measure
estimates clear
estpost tab prior_uncertainty_des
esttab $output cells("b pct(fmt(2)) cumpct(fmt(2))") noobs nomtitle nonum note("Note: Mean=1.68, SD=1.03).") varwidth(25)

**Table A4: Effect of Intervention on Posterior Level of Action Without and Without Prior Level of Action and Prior Beliefs as Covariates
estimates clear
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std  $sample, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "No"
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0  $sample, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "No"
qui: eststo: reg level_t4 treatment##c.gap_t4 prior_beliefs_std  $sample, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "No"
qui: eststo: reg level_t4 treatment##c.gap_t4   $sample, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "No"
esttab $output nobase compress scalars("St Strata FE" "C Covariates" "r2_a Adjusted R^2") sfmt(2) b(a2) se label varwidth(40) nomtitle nogap order(gap_t4 1.treatment 1.treatment#c.gap_t4 level_t0   prior_beliefs_std )

**Table A5: Effect of Intervention on Posterior Level of Action with Teacher Covariates
estimates clear
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std $covariates $missing  $sample, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "Yes"
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std i.strata $covariates $missing  $sample, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "Yes"
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std i.test_use test_use_m experience experience_m $covariates $missing  $sample, cluster($cluster)
qui	estadd local St "No"
qui	estadd local C "Yes"
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std i.strata i.test_use test_use_m experience experience_m $covariates $missing  $sample, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "Yes"
esttab  $output drop(*strata $missing test_use_m experience_m ) nobase compress scalars("St Strata FE" "C Covariates" "r2_a Adjusted R^2") sfmt(2) b(a2) se label varwidth(40) nomtitle nogap order(gap_t4 1.treatment 1.treatment#c.gap_t4 level_t0   prior_beliefs_std )

*Figure A1: Relationship between Absolute Gap and Level of Change in Direction of the Gap across Tretament Conditions
reg level_direction treatment##c.abs_gap , cluster($cluster)
margins, at( abs_gap =(0(0.1)2) treatment=(0 1))
marginsplot,  plot1opts(recast(line) lcolor(gray)) plot2opts(recast(line) lpattern(solid)) recastci(rarea) ciopts(fcolor(%20) lcolor(%20))  scheme(s1mono) ///
xtitle(Absolut gap) ytitle (Level change in direction of gap) title(" ", ring(0)) name(level_d, replace) ///
legend(order(4 "Treatment" 3 "Control" )  rows(2) region(lpattern(blank)) position(1) ring(0)) ///
xlabel(0(1)2) ylabel(0(.05).18)

*Table A6:  Effect of Intervention on Posterior Level of Action for each time period
estimates clear
qui: eststo: reg level_t4 treatment##c.gap_t4 c.level_t0 prior_beliefs_std i.strata  $sample, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "Yes"
qui: eststo: reg level_t3 treatment##c.gap_t3 c.level_t0 prior_beliefs_std i.strata  $sample, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "Yes"
qui: eststo: reg level_t2 treatment##c.gap_t2 c.level_t0 prior_beliefs_std i.strata  $sample, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "Yes"
qui: eststo: reg level_t1 treatment##c.gap_t1 c.level_t0 prior_beliefs_std i.strata  $sample, cluster($cluster)
qui	estadd local St "Yes"
qui	estadd local C "Yes"
esttab  $output drop(*strata) nobase compress scalars("St Strata FE" "C Covariates" "r2_a Adjusted R^2") sfmt(2) b(a2) se label varwidth(40) nomtitle nogap order(gap_t4 1.treatment 1.treatment#c.gap_t4 pre_level  prior_beliefs_std )

*Table A7: Effect of Intervention on Change in Level of Difficulty in the Direction of the Gpa. Moderated by Absolute Gap
estimates clear
eststo: reg level_direction treatment##c.abs_gap , cluster($cluster)
esttab $output sfmt(2) b(a2) se label varwidth(40) nomtitle nogap nobase

*Table A8: Effect of Intervention on Change in Level of Difficulty in the Direction of the Gpa. Moderated by Absolute Information Signal Consistency
estimates clear
eststo: reg level_direction treatment##c.abs_consistency , cluster($cluster)
esttab $output sfmt(2) b(a2) se label varwidth(40) nomtitle nogap nobase

*Table A9: Effect of Intervention on Posterior Gap. Moderated by Non-Western Student
estimates clear
eststo: reg abs_posterior_gap 	i.treatment i.strata  $sample & non_western_m==0, cluster($cluster)
qui	estadd local St "Yes"
eststo: reg abs_posterior_gap 	i.non_western_nomiss $sample & non_western_m==0 & treatment==0, cluster($cluster)
qui	estadd local St "No"
eststo: reg abs_posterior_gap 	treatment##non_western_nomiss strata  $sample & non_western_m==0, cluster($cluster)
qui	estadd local St "Yes"
esttab $output drop(*strata) sfmt(2) b(a2) se label varwidth(40) nomtitle nogap nobase scalars("St Strata FE" "r2_a Adjusted R^2")

*Table A10: Effect of Intervention on Reading Skills
estimates clear
eststo: reg testresult_t4 		treatment   i.strata  $sample, cluster($cluster)
qui	estadd local St "Yes"
esttab $output drop(*strata) sfmt(2) b(a2) se label varwidth(40) nomtitle nogap nobase scalars("St Strata FE" "r2_a Adjusted R^2")
